home *** CD-ROM | disk | FTP | other *** search
/ Shareware Grab Bag / Shareware Grab Bag.iso / 007 / bastips4.arc / BASICNTS.TXT next >
Encoding:
Text File  |  1987-03-03  |  12.5 KB  |  335 lines

  1.                      The Root of the Problem
  2.        (PC Tech Journal January 1987 by Murray L. Lesser)
  3.  
  4.      Building an intricate graphics program requires evaluation of
  5. thousands of square roots, but only to integer accuracy.  BASIC
  6. compilers do it the hard way: they convert the integer function
  7. argument to a single-precision, floating-point number, take the square
  8. root of that, then convert the floating-point result back into an
  9. integer.
  10.      ROOT.ASM produces the nearest integer to the actual square root
  11. of an integer argument.  It returns a zero value for a negative
  12. argument rather than an error.  Because the routine has no absolute
  13. jumps, it could be used to speed up interpreted BASIC if it were
  14. coded into an integer array.  ROOT also can be BLOADed into an
  15. interpreted BASIC program.
  16.      An example of ROOT in use under Microsoft's QuickBASIC is given
  17. in ROOTTEST.ASM.  ROOTTEST runs four times faster using ROOT than it
  18. does using QuickBASIC's floating-point, square-root function.
  19.  
  20. ; ROOT.ASM -- an assembled fast integer square root subroutine to be
  21. ;   linked to Microsoft compiled BASIC programs.
  22. ; Call with ROOT(x,y) where x is an integer expression
  23. ;                           y is an integer variable
  24. ; The square root of x will be returned in y.  (Negative input will
  25. ;   return a zero.)
  26. ; ROOT is a binary adaptation of the synthetic division square root
  27. ;  procedure shown in the paper by J. E. Meggit, "Pseudo Division and
  28. ;  Pseudo Multiplication Processes."  The IBM Journal of Research and
  29. ;  Development, vol. 6, no. 2 (April 1962), pp. 210-226.
  30. ; Written by M. L. Lesser, April 13, 1986.
  31. ; Assembled with Microsoft MASM version 4.00
  32. ;
  33. DATA    SEGMENT BYTE PUBLIC 'CODE'
  34. DATA    ENDS
  35. DGROUP    GROUP DATA
  36.  
  37. SQRT    SEGMENT BYTE PUBLIC 'CODE'
  38.     ASSUME CS:SQRT,DS:DGROUP
  39.     PUBLIC ROOT
  40.  
  41. ROOT    PROC FAR
  42.     PUSH    BP
  43.     MOV    BP,SP
  44.     MOV    BX,8[BP]
  45.     MOV    AX,[BX]
  46.     XOR    DX,DX
  47.     MOV    CX,8
  48.     MOV    BX,4000H
  49.     MOV    DI,8000H
  50.     MOV    SI,2000H
  51.     CMP    AX,DX
  52.     JLE    DONE
  53. DOIT:    SHL    DX,1
  54.     CMP    AX,BX
  55.     JL    NEXT
  56.     INC    DX
  57.     SUB    AX,BX
  58.     ADD    BX,DI
  59. NEXT:    SUB    BX,SI
  60.     SHR    DI,1
  61.     SHR    DI,1
  62.     SHR    SI,1
  63.     SHR    SI,1
  64.     SHR    BX,1
  65.     LOOP    DOIT
  66.     CMP    AX,BX
  67.     JLE    DONE
  68.     INC    DX
  69. DONE:    MOV    BX,6[BP]
  70.     MOV    [BX],DX
  71.     POP    BP
  72.     RET    4
  73.  
  74. ROOT    ENDP
  75. SQRT    ENDS
  76.     END
  77.  
  78.  
  79. ROOTTTEST.ASM:
  80.  
  81. ' TEST.BAS: An example of the use of ROOT.ASM.
  82. ' Compile with a Microsoft BASIC compiler and link to ROOT.OBJ
  83.  
  84.     defint a-c
  85.     defsng d
  86.     for a=0 to 32766
  87.       call root(a,b)
  88.       let d=sqr(a)
  89.       let c=d
  90.       gosub 1000
  91.       if inkey$=chr$(3) then end
  92.     next a
  93.     let a=32767
  94.     call root(a,b)
  95.     let=sqr(a)
  96.     let c=d
  97.     gosub 1000
  98. end
  99. 1000     print a,b,c,d
  100.          if b<>c then while inkey$<>"":wend
  101.          return
  102.  
  103. -----------------------------------------------------------------
  104.                  Using PUT and GET on the IBM PC
  105.         (COMPUTE! Magazine March 1987 by Rafael Gonzalez)
  106.  
  107.      IBM BASIC has two commands -- PUT and GET -- that make it easy for
  108. you to animate figures.  These powerful commands appear frequently in
  109. games, but they have many other uses, as well.  (PUT and GET are also
  110. used for random file operations, but with a different syntax.)
  111.      You might think of PUT and GET as "bit pump" operations which move
  112. bits from memory onto the screen (PUT) and from the screen into memory
  113. (GET).  GET reads the colors of the points within a rectangular screen
  114. area and stores that information in an array.  The basic syntax for the
  115. command is:
  116.  
  117. GET(x1,y1)-(x2,y2),array
  118.  
  119.      Each GET command includes two pairs of screen coordinates and an
  120. array name.  The coordinates define the area to be captured and the
  121. array name tells BASIC where to store the image.  The first pair of
  122. coordinates (x1 and y1 in this example) defines the upper left corner
  123. of the rectangle.  The second pair (x2 and y2) defines the rectangle's
  124. lower right corner.  (This is identical to the method used to define a
  125. rectangle in a LINE command with the B option.)
  126.      The array used with GET must be of the numeric type.  It can be
  127. any precision, although integer arrays are commonly used.  Except for
  128. very small shapes, you must DIMension the array before using it.  This
  129. task, in turn, requires that you calculate how big the array should be.
  130. The BASIC formula for calculating the array size is:
  131.  
  132. 4 + INT((x * bits per pixel + 7) / 8) * y
  133.  
  134.      In this case, x and y are the lengths of the horizontal and
  135. vertical sides of the rectangle, respectively.  The bits per pixel
  136. value is equal to 4 in low resolution, 2 or 4 in medium resolution,
  137. and 1 or 2 in high resolution, depending upon the current screen mode.
  138.      For example, suppose you want to capture a 10 x 12-pixel image in
  139. medium resolution with GET.  The number of bytes required is 4 + INT
  140. ((10 * 2 + 7) / 8) * 12, or 40 bytes.  Next, you must consider how many
  141. bytes each element of the array contains.  This factor depends on the
  142. array's precision.  This table shows how many bytes are contained in
  143. each element of an integer, single-precision, or double-precision array:
  144.  
  145. Bytes    Array Type
  146.  
  147.   2    Integer
  148.   4    Single-precision
  149.   8    Double-precision
  150.  
  151.      Since the example shape requires 40 bytes, it can be stored in an
  152. integer array containing 20 elements, a single-precision array
  153. containing 8 elements, or a double-precision array containing 5
  154. elements.  It's important to dimension an array of the proper size,
  155. since BASIC stops with the error message, "Illegal function call" if
  156. the array is too small.  Using an overly large array doesn't do any
  157. harm.  However, grossly overlarge arrays waste memory.
  158.      The PUT command is the opposite of GET: Once you have stored a
  159. shape with GET, PUT can place the shape anywhere on the screen.  The
  160. syntax is:
  161.  
  162. PUT (x,y),array,action
  163.  
  164.      In this example, x and y set the coordinates where the upper left
  165. corner of the image will be placed and the variable array identifies
  166. the array which contains the shape.
  167.      The optional parameter action lets you select different modes for
  168. a PUT operation.  This part of the statement may consist of the word
  169. PSET, PRESET, XOR, OR, or AND.  If you omit the action parameter, PUT
  170. defaults to XOR mode.
  171.      The PUT mode determines how the placed shape interacts with
  172. graphics data that's already present in the same screen area.  Type in
  173. and save the example program, then run it to see how the mode affects
  174. PUT.  The program draws a multicolored background and PUTs the same
  175. sahep on the screen in five different places using all of the different
  176. modes.  Here's what each mode does:
  177.  
  178.     PSET.  In this mode, PUT simply stores the captured data on
  179. the screen, overwriting any graphics data that previously existed in
  180. the same area.  In the example program the transferred image completely
  181. replaces the contents of that screen area.
  182.  
  183.     PRESET.  This mode replaces all existing data, just as in PSET
  184. mode, but the image is reversed.  That is, a value of 0 in the array
  185. causes the corresponding point on the screen to have attribute number
  186. 3, and vice versa.  A value of 1 in the array causes the corresponding
  187. point on the screen to have attribute 2, and so forth.  In the program,
  188. this mode causes the image to have a different color.
  189.  
  190.     AND.  The AND mode sets pixels only at points that already
  191. contain data matching corresponding data in the transferred image.
  192. In the example program, only pixels that are originally cyan remain
  193. in the final image.
  194.  
  195.     OR.  This mode superimposes an image onto existing data.
  196.  
  197.     XOR.  The XOR mode is most often used for animation.  When a
  198. pixel in the PUT image overlays a point on the screen that contains
  199. data, the point is inverted.  This feature allows you to move a shape
  200. nondestructively over a complex background:  When an image is PUT
  201. against a background twice, it restores the original data unchanged.
  202.  
  203.      The following tables show how AND, XOR, and OR modes affect
  204. screen attributes in medium-resolution mode (SCREEN 1 or SCREEN 4).
  205.  
  206. AND
  207. ---               Array Value
  208. screen        0    1    2    3
  209.  
  210.   0        0    0    0    0
  211.   1        0    1    0    1
  212.   2        0    0    2    2
  213.   3        0    1    2    3
  214.  
  215. OR
  216. --               Array Value
  217. screen        0    1    2    3
  218.  
  219.   0        0    1    2    3
  220.   1        1    1    3    3
  221.   2        2    3    2    3
  222.   3        3    3    3    3
  223.  
  224. XOR
  225. ---               Array Value
  226. screen        0    1    2    3
  227.  
  228.   0        0    1    2    3
  229.   1        1    0    3    2
  230.   2        2    3    0    1
  231.   3        3    2    1    0
  232.  
  233.      The example program also demonstrates simple animation with PUT
  234. in XOR mode.  After the five large shapes are drawn, it sends a small
  235. shape bouncing around the screen.  The process of animation involves
  236. four basic steps:
  237.     1. Calculate a new position for the shape.
  238.     2. PUT the shape on the screen at its previous location (to
  239.        erase the old image).
  240.     3. PUT the shape in its new position.
  241.     4. Return to step 1.
  242.      Before you enter the loop, you must have PUT the shape on the
  243. screen once, so that the PUT in step 2 will erase it.  This preliminary
  244. step is performed in line 440 of the program.  Line 450 saves the old
  245. position of the shape in OLDX and OLDY before a new position is
  246. calculated in lines 460-490.
  247.      BASIC animation with PUT always involves a certain amount of
  248. flickering, which results from the delay between the time the old shape
  249. is erased and the new one is drawn.  To minimize flicker, you should
  250. perform the two PUTs as close together as possible.  This reduces the
  251. amount of time that the shape is invisible.  The example program
  252. accomplishes this by putting both PUT statements on the same line.
  253. The first statement in line 510 erases the old image, and the second
  254. statement draws the new one.  The do-nothing loop in line 520 holds
  255. the new image on the screen for a short interval to alleviate flicker
  256. even further.  Most programs won't need an explicit delay, since the
  257. program will be doing time-consuming tasks between each redraw.
  258.      Once you understand the basics of GET and PUT, you may find many
  259. uses for these commands.  A drawing program, for instance, may include
  260. a feature allowing you to copy one screen area to another.  If you
  261. capture the indicated area with GET, it is effectively saved in an
  262. offscreen buffer, and can be replaced at any time with a simple PUT
  263. command.  In fact, if sufficient memory is available, you can even
  264. GET an entire screen.
  265.      To see the effect of a full-screen GET, press any key while the
  266. small box is moving on the screen.  The program saves the current
  267. screen in the array SCRN2%, then PUTs on the screen an image previously
  268. stored in the array SCRN1%.  Immediately thereafter, it restores the
  269. current image by PUTting the SCRN2% image back on the screen.  As you
  270. can see, significant delays result from manipulating images of this
  271. size.
  272.  
  273. 100 SCREEN 1:KEY OFF:RANDOMIZE TIMER
  274. 110 LINE (60,60)-(120,120),1,BF
  275. 120 X=61:Y=61
  276. 130 BITSPERPIXEL=2
  277. 140 NUM=4+INT((X*BITSPERPIXEL+7)/8)*Y
  278. 150 DIM LARGE%(NUM/2),SMALL%(NUM/4)
  279. 160 X=320:Y=200
  280. 170 NUM=4+INT((X*BITSPERPIXEL+7)/8)*Y
  281. 180 DIM SCRN1%(NUM/2),SCRN2%(NUM/2)
  282. 190 GET (60,60)-(120,120),LARGE%
  283. 200 GET (60,60)-(90,90),SMALL%
  284. 210 CLS
  285. 220 FOR COL=1 TO 3:FOR J=1 TO 50
  286. 230 X=INT(RND*319):Y=INT(RND*199)
  287. 240 PSET (X,Y),COL:NEXT:NEXT
  288. 250 GET (0,0)-(319,199),SCRN1%
  289. 260 FOR J=40 TO 140 STEP 10
  290. 270 LINE (0,J)-(320,J+1),1,BF
  291. 280 LINE (0,J+2)-(320,J+3),2,BF
  292. 290 LINE (0,J+4)-(320,J+5),3,BF
  293. 300 NEXT
  294. 310 PUT (30,20),LARGE%,PSET
  295. 320 PUT (120,20),LARGE%,PRESET
  296. 330 PUT (210,20),LARGE%,AND
  297. 340 PUT (70,110),LARGE%,OR
  298. 350 PUT (180,110),LARGE%,XOR
  299. 360 LOCATE 2,6:PRINT "PSET"
  300. 370 LOCATE 2,17:PRINT "PRESET"
  301. 380 LOCATE 2,29:PRINT "AND"
  302. 390 LOCATE 23,13:PRINT "OR"
  303. 400 LOCATE 23,26:PRINT "XOR"
  304. 410 X=10:Y=50:DX=2:DY=2
  305. 420 RLIM=320-32:LLIM=0
  306. 430 ULIM=0:DLIM=200-32
  307. 440 PUT (X,Y),SMALL%
  308. 450 OLDX=X:OLDY=Y
  309. 460 IF INKEY$<>"" THEN GET (0,0)-(319,199),SCRN2%:PUT (0,0),SCRN1%,AND:PUT (0,0),SCRN2%,PSET
  310. 470 X=X+DX
  311. 480 IF X=>RLIM OR X<=LLIM THEN DX=-DX
  312. 490 Y=Y+DY
  313. 500 IF Y<=ULIM OR Y>=DLIM THEN DY=-DY
  314. 510 PUT (OLDX,OLDY),SMALL%:PUT (X,Y),SMALL%
  315. 520 FOR J=0 TO 80:NEXT
  316. 530 GOTO 450
  317.  
  318. -----------------------------------------------------------------
  319.                       Sowing a Random FIELD
  320.               (PC World March 1987 The Help Screen)
  321.  
  322.      BASIC 2.0 supports random file record lengths up to 32,767 bytes.
  323. The problem is using a random file structure to store 2048 single-
  324. precision values (8192 bytes) per record.
  325.      The trick is to use multiple FIELD statements.  Consider:
  326.  
  327. 1 OPEN "FOO" AS #1
  328. 2 FIELD 1,100 AS A$,200 AS B$
  329. 3 FIELD 1,300 AS DUMMY$,40 AS C$
  330.  
  331. Note that line 3 uses a dummy variable to move the file buffer's
  332. pointer past the area assigned for A$ and B$ (100 + 200 = 300) so
  333. that the field definitions of line 2 are not lost.
  334.  
  335.